/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  OnRcvScrollListener.java   
 * @Package com.jiyi.inteface   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-10-23 下午8:12:06   
 * @version V1.0     
 */
package com.jiyi.inteface;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

/**
 * @ClassName: OnRcvScrollListener
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-10-23 下午8:12:06
 * 
 */
public class OnRcvScrollListener extends RecyclerView.OnScrollListener
		implements OnBottomListener {

	private String TAG = getClass().getSimpleName();

	public static enum LAYOUT_MANAGER_TYPE {
		LINEAR, GRID, STAGGERED_GRID
	}

	/**
	 * layoutManager的类型（枚举）
	 */
	protected LAYOUT_MANAGER_TYPE layoutManagerType;

	/**
	 * 最后一个的位置
	 */
	private int[] lastPositions;

	/**
	 * 最后一个可见的item的位置
	 */
	private int lastVisibleItemPosition;
	/*    *//**
	 * 是否正在加载
	 */
	/*
	 * private boolean isLoadingMore = false;
	 */

	/**
	 * 当前滑动的状态
	 */
	private int currentScrollState = 0;

	@Override
	public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
		super.onScrolled(recyclerView, dx, dy);

		RecyclerView.LayoutManager layoutManager = recyclerView
				.getLayoutManager();
		// int lastVisibleItemPosition = -1;
		if (layoutManagerType == null) {
			if (layoutManager instanceof LinearLayoutManager) {
				layoutManagerType = LAYOUT_MANAGER_TYPE.LINEAR;
			} else if (layoutManager instanceof GridLayoutManager) {
				layoutManagerType = LAYOUT_MANAGER_TYPE.GRID;
			} else if (layoutManager instanceof StaggeredGridLayoutManager) {
				layoutManagerType = LAYOUT_MANAGER_TYPE.STAGGERED_GRID;
			} else {
				throw new RuntimeException(
						"Unsupported LayoutManager used. Valid ones are LinearLayoutManager, GridLayoutManager and StaggeredGridLayoutManager");
			}
		}

		switch (layoutManagerType) {
		case LINEAR:
			lastVisibleItemPosition = ((LinearLayoutManager) layoutManager)
					.findLastVisibleItemPosition();
			break;
		case GRID:
			lastVisibleItemPosition = ((GridLayoutManager) layoutManager)
					.findLastVisibleItemPosition();
			break;
		case STAGGERED_GRID:
			StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
			if (lastPositions == null) {
				lastPositions = new int[staggeredGridLayoutManager
						.getSpanCount()];
			}
			staggeredGridLayoutManager
					.findLastVisibleItemPositions(lastPositions);
			lastVisibleItemPosition = findMax(lastPositions);
			break;
		}

	}

	@Override
	public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
		super.onScrollStateChanged(recyclerView, newState);
		currentScrollState = newState;
		RecyclerView.LayoutManager layoutManager = recyclerView
				.getLayoutManager();

		//判断下拉刷新的时候不触发加载更多的事件
		int firstPos = ((LinearLayoutManager) layoutManager)
				.findFirstCompletelyVisibleItemPosition();
		if (firstPos > 0) {
			int visibleItemCount = layoutManager.getChildCount();
			int totalItemCount = layoutManager.getItemCount();
			if ((visibleItemCount > 0
					&& currentScrollState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItemPosition) >= totalItemCount - 1)) {
				// Log.d(TAG, "is loading more");
				onBottom();
			}
		}

	}

	@Override
	public void onBottom() {
		// Log.d(TAG, "is onBottom");
	}

	private int findMax(int[] lastPositions) {
		int max = lastPositions[0];
		for (int value : lastPositions) {
			if (value > max) {
				max = value;
			}
		}
		return max;
	}
}
